package org.jdemo.search.lucene; /**
 *    系统名称：17173内容管理系统.
 *    作者：陈宇翔
 *    (C) Copyright cyou corporation 2014  All Rights Reserved.
 *    注意： 本内容仅限于软件公司内部使用，禁止转发
 */

import com.cyou.fz.testdata.generator.WordUtil;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;

/**
 * PACKAGE_NAME.
 * <p>User:宇翔
 * <p>Date:14-3-31  下午2:57
 */
public class QueryDemo {
    public static void main(String[] args) throws IOException, ParseException {
        Directory directory = FSDirectory.open(new File("F:\\temp\\luceneidx"));
        DirectoryReader ireader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(ireader);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);

        String[] multiFields = {"author"};
        MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_47, multiFields, analyzer);

        long totalTime = 0;
        for (int i = 0; i < 1000; i++) {
            long startTime = System.currentTimeMillis();

            String word = WordUtil.randomZHWord();
            int docLength = searcher.search(parser.parse(word), null, 10).scoreDocs.length;

            long endTime = System.currentTimeMillis();
            System.out.println("查询【" + word + "】耗时: " + (endTime - startTime) +  "ms" + " 共" + docLength + "条数据");
            totalTime += (endTime - startTime);
        }

        System.out.println("总耗时：" + totalTime + "ms");
        ireader.close();
        directory.close();
    }
}
